package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.cardone.common.dto.PaginationDto;
import com.cardone.persistent.CommonDtoAttributes;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelBuilder;
import com.cardone.persistent.builder.ModelBuilderUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.UsOrgTranslateDto;

/**
 * 用户组织调动
 * 
 * @author yaoht
 * 
 */
@Getter
@Setter
public class UsOrgTranslateJdbcDao implements UsOrgTranslateDao {
	/**
	 * jdbcDao模板支持
	 */
	private JdbcTemplateSupport jdbcTemplateSupport;

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int deleteByCode(@NonNull UsOrgTranslateDto deleteUsOrgTranslate, String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(deleteUsOrgTranslate, whereAndEqProperties));

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.update(SqlIds.deleteByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int deleteByCodeOrId(@NonNull UsOrgTranslateDto deleteUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(deleteUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.deleteByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int deleteByIds(@NonNull UsOrgTranslateDto deleteUsOrgTranslate) {
		ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder();

		modelBuilder.put(new String[] { DtoAttributes.ids.name() }, deleteUsOrgTranslate.getIds());

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.deleteByIds.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_4)
	public <P> P findByCode(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findUsOrgTranslate, String[] selectProperties,
	        String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(findUsOrgTranslate, whereAndEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.Constants.select.stringValue(), selectProperties);

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.find(mappedClass, SqlIds.findByCode.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <P> P findByCodeOrId(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(findUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.find(mappedClass, SqlIds.findByCode.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <P> P findByName(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(findUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.find(mappedClass, SqlIds.findByName.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_4)
	public <P> List<P> findListByCode(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findListUsOrgTranslate,
	        String[] selectProperties, String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils
		        .newModelBuilder(new ModelArgs(findListUsOrgTranslate, whereAndEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.Constants.select.stringValue(), selectProperties);

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.findList(mappedClass, SqlIds.findByCode.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <P> List<P> findListByCodeOrId(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findListUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(findListUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.findList(mappedClass, SqlIds.findByCode.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <P> List<P> findListByName(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto findListUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(findListUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.findList(mappedClass, SqlIds.findByName.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int insertByCode(@NonNull UsOrgTranslateDto insertUsOrgTranslate, String... insertProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.Constants.insert.stringValue(),
		        insertUsOrgTranslate, insertProperties));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.insertByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int insertByCodeOrId(@NonNull UsOrgTranslateDto insertUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.Constants.insert.stringValue(),
		        insertUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.insertByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int insertByName(@NonNull UsOrgTranslateDto insertUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.Constants.insert.stringValue(),
		        insertUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.insertByName.id(), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <P> PaginationDto<P> paginationByCode(@NonNull Class<P> mappedClass, @NonNull UsOrgTranslateDto paginationUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(paginationUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.pagination(mappedClass, new PaginationArgs(SqlIds.readByCode.id(), SqlIds.findByCode.id(),
		        paginationUsOrgTranslate.getPaginationNo(), paginationUsOrgTranslate.getPaginationSize()), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationByName(@NonNull UsOrgTranslateDto paginationUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(paginationUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.pagination(new PaginationArgs(SqlIds.readByCode.id(), SqlIds.findByCode.id(),
		        paginationUsOrgTranslate.getPaginationNo(), paginationUsOrgTranslate.getPaginationSize()), modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_4)
	public <R> R readByCode(@NonNull Class<R> requiredType, String objectId, @NonNull UsOrgTranslateDto readUsOrgTranslate,
	        String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(readUsOrgTranslate, whereAndEqProperties));

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.read(requiredType, SqlIds.readByCode.id(), objectId, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_2)
	public <R> R readByCodeNqId(@NonNull Class<R> requiredType, @NonNull UsOrgTranslateDto readUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder();

		// final ModelBuilder modelBuilder =
		// ModelBuilderUtils.newModelBuilder().putExtend(null,
		// CommonDtoAttributes.code.name(), null,
		// readUsOrgTranslate.getCode());

		// if (StringUtils.isNotBlank(readUsOrgTranslate.getId())) {
		// modelBuilder.putExtend(ModelBuilder.WHERE_AND_NQ,
		// CommonDtoAttributes.id.name(), null, readUsOrgTranslate.getId());
		// }

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.read(requiredType, SqlIds.readByCode.id(), null, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_3)
	public <R> R readByCodeOrId(@NonNull Class<R> requiredType, String objectId, @NonNull UsOrgTranslateDto readUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(readUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.read(requiredType, SqlIds.readByCode.id(), objectId, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_3)
	public <R> R readByName(@NonNull Class<R> requiredType, String objectId, @NonNull UsOrgTranslateDto readUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(readUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.read(requiredType, SqlIds.readByName.id(), objectId, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_4)
	public <R> List<R> readListByCode(@NonNull Class<R> requiredType, String objectId,
	        @NonNull UsOrgTranslateDto readListUsOrgTranslate, String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils
		        .newModelBuilder(new ModelArgs(readListUsOrgTranslate, whereAndEqProperties));

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.readList(requiredType, SqlIds.readByCode.id(), objectId, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_3)
	public <R> List<R> readListByCodeOrId(@NonNull Class<R> requiredType, String objectId,
	        @NonNull UsOrgTranslateDto readListUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(readListUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.readList(requiredType, SqlIds.readByCode.id(), objectId, modelBuilder.build());
	}

	@Override
	@Cacheable(value = UsOrgTranslateDao.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_3)
	public <R> List<R> readListByName(@NonNull Class<R> requiredType, String objectId, @NonNull UsOrgTranslateDto readListUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(readListUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.readList(requiredType, SqlIds.readByName.id(), objectId, modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public void reload() {
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int updateByCode(@NonNull UsOrgTranslateDto updateUsOrgTranslate, String[] updateProperties, String... whereAndEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.Constants.update.stringValue(),
		        updateUsOrgTranslate, updateProperties), new ModelArgs(updateUsOrgTranslate, whereAndEqProperties));

		if (!(ArrayUtils.contains(whereAndEqProperties, CommonDtoAttributes.beginDate.name()) || ArrayUtils.contains(
		        whereAndEqProperties, CommonDtoAttributes.endDate.name()))) {
			modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());
		}

		return this.jdbcTemplateSupport.update(SqlIds.updateByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int updateByCodeOrId(@NonNull UsOrgTranslateDto updateUsOrgTranslate) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(updateUsOrgTranslate));

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.updateByCode.id(), modelBuilder.build());
	}

	@Override
	@CacheEvict(value = UsOrgTranslateDao.BEAN_ID, allEntries = true)
	public int updateByIds(@NonNull UsOrgTranslateDto updateUsOrgTranslate) {
		ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder();

		modelBuilder.put(new String[] { CommonDtoAttributes.endDate.name() }, new DateTime().plusSeconds(-1));

		modelBuilder.put(new String[] { DtoAttributes.ids.name() }, updateUsOrgTranslate.getIds());

		modelBuilder.putTrue(ModelBuilder.Constants.whereAndBetween.stringValue(), CommonDtoAttributes.sysdate.name());

		return this.jdbcTemplateSupport.update(SqlIds.updateByIds.id(), modelBuilder.build());
	}
}